package com.mrent.web.controller.front;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import com.mrent.entity.Goods;
import com.mrent.entity.Member;
import com.mrent.entity.Order;
import com.mrent.entity.OrderManager;
import com.mrent.service.GoodsService;
import com.mrent.service.MemberService;
import com.mrent.service.OrderManagerService;
import com.mrent.service.OrderService;
import com.mrent.service.Page;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;
import com.mrent.web.controller.pay.PayNotifyController;

@Controller
public class IndexController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private OrderManagerService orderManagerService;
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private MemberService memberService;
	
	private static java.util.logging.Logger logger = Logger.getLogger(IndexController.class.getName());
	
	/**
	 * 前台首页.
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpSession session,ModelMap model){
		Member member = (Member)session.getAttribute(Constance.SESSION_MEMBER_NAME);
		//取用户申请确认的订单数.
		if(member != null){
			List<Order> myOrders = orderService.getOrderByUser(member.getMemberName());
			model.addAttribute("myOrderTotal", myOrders.size());
		}
		
		
		//取店主信息.
		OrderManager om = getShopManager();
		if(om != null){
			Member managerUser = getMemberInfo(om.getOrderMember());
			model.addAttribute("manager", om);
			model.addAttribute("managerUser", managerUser);
			
			//取10拍卖商品.
			List<Goods> auctionGoods10 = getAuctionGoods(om.getOrderMember());
			model.addAttribute("auctionGoods10", auctionGoods10);
			
			//取10最新商品，不包括拍卖商品.
			List<Goods> newGoods10 = getNewGoods(om.getOrderMember());
			model.addAttribute("newGoods10", newGoods10);
			
			//取镇店之宝.
			Goods shopBaby = getShopBaby(om.getOrderMember());
			model.addAttribute("shopBaby", shopBaby);
		}
		
		
		return "front/index";
	}
	
	
	
	/**
	 * 取当日店主.
	 * 
	 * @return
	 */
	private OrderManager getShopManager(){
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		Integer year = calendar.get(Calendar.YEAR);
		Integer month = calendar.get(Calendar.MONTH) +1;
		Integer day = calendar.get(Calendar.DATE);
		logger.info("current date:"+year+"-"+month+"-"+day);
		List<OrderManager> list = orderManagerService.getManagerByDate(year, month, day);
		logger.info("-----shop manager----list:" +list.size());
		OrderManager om = null;
		if(list.size() >0){
			om = list.get(0);
		}
		return om;
	}
	
	/**
	 * 取店主相关信息.
	 * @return
	 */
	private Member getMemberInfo(String memberName){
		Member member = memberService.findByMemberName(memberName);
		return member;
	}
	
	/**
	 * 取10拍卖商品.
	 * 
	 * @return
	 */
	private List<Goods> getAuctionGoods(String manager){
		Page page = new Page();
		page.setPageSize(10);
		List<Goods> goods = goodsService.listAuctionGoods(page, manager);
		return goods;
	}
	
	/**
	 * 取最新商品，不包括拍卖商品.
	 * 
	 * @param manager
	 * @return
	 */
	private List<Goods> getNewGoods(String manager){
		Page page = new Page();
		page.setPageSize(10);
		List<Goods> goods = goodsService.listNewGoods(page, manager);
		return goods;
	}
	
	/**
	 * 取镇店之宝.
	 * 
	 * @param manager
	 * @return
	 */
	private Goods getShopBaby(String manager){
		Goods goods = goodsService.getShopBaby(manager);
		return goods;	
	}
	

}
